<?php 

/**
* 数据库操作类
*/
class Model 
{
	public static $db;//pdo数据库链接对象
	public $table;//表名
	public $error;//错误信息
	public $errno;//错误代码
	public $where;//where条件
	/**
	 * [__construct 实例化pdo对象]
	 * @Author   Xuebingsi
	 * @DateTime 2017-03-24T11:44:22+0800
	 * @param    [type]                   $table [表名]
	 */
	function __construct($table=null)
	{
		if($table){
			$this->table=$table;
		}
		//判断是否有pdo对象
		if(!self::$db){
			var_dump($table);
			try{
				self::$db = new PDO("mysql:host=".C('db_host').";dbname=".C('db_dbname').";charset=utf8",C('db_user'),C('db_password'));
		
			}catch(PDOException  $e ){
				echo "Error: ".$e;exit;
			}
		}
			

	}

	/**
	 * [add 增加功能]
	 * @Author   Xuebingsi
	 * @DateTime 2017-03-24T11:45:51+0800
	 * @param    [arr]                   $data [增加的数据]
	 */
	public function add($data)
	{
		// var_dump($data);
		
		$values = implode("','", $data);
		$keys = implode(',', array_keys($data));

		// var_dump($values);
		// var_dump($keys);

		$sql = "insert into ".$this->table." (".$keys.") values('".$values."')";

		return $this->exec($sql);
	}

	/**
	 * [exec pdo执行方法（增，删，改）]
	 * @Author   Xuebingsi
	 * @DateTime 2017-03-24T11:46:11+0800
	 * @param    [type]                   $sql [需要执行的语句]
	 * @return   [type]                        [返回ture,flase]
	 */
	public function exec($sql)
	{
		$res =self::$db->exec($sql);

		if(!$res){
			$this->error=$this->db->errorInfo()[2];
			$this->errno=$this->db->errorCode();
		}
		return $res;
	}

	/**
	 * [query pdo执行(查询类的)]
	 * @Author   Xuebingsi
	 * @DateTime 2017-03-24T11:47:09+0800
	 * @param    [type]                   $sql [查询的sql]
	 * @return   [type]                        [结果集]
	 */
	public function query($sql)
	{
		$res =self::$db->query($sql);

		if(!$res){
			$this->error=$this->db->errorInfo()[2];
			$this->errno=$this->db->errorCode();
			return false;
		}

		return $res->fetchAll(PDO::FETCH_ASSOC);
	}

	/**
	 * [getError 获得错误信息]
	 * @Author   Xuebingsi
	 * @DateTime 2017-03-24T11:47:42+0800
	 * @return   [type]                   [description]
	 */
	public function getError()
	{
		return "错误代码：".$this->errno."错误信息：".$this->error;
	}

	/**
	 * [where 加条件执行]
	 * @Author   Xuebingsi
	 * @DateTime 2017-03-24T11:47:56+0800
	 * @param    [type]                   $where [description]
	 * @return   [type]                          [description]
	 */
	public function where($where)
	{
		$this->where = " where ".$where;
		return $this;
	}

	/**
	 * [find 查询功能]
	 * @Author   Xuebingsi
	 * @DateTime 2017-03-24T11:48:19+0800
	 * @return   [type]                   [description]
	 */
	public function find()
	{
		$sql = "select * from ".$this->table.$this->where;
		return $this->query($sql);
	}

	/**
	 * [save 更新功能]
	 * @Author   Xuebingsi
	 * @DateTime 2017-03-24T11:48:29+0800
	 * @param    [type]                   $data [description]
	 * @return   [type]                         [description]
	 */
	public function save($data)
	{
		// var_dump($data);

		$str = '';

		foreach ($data as $k => $v) {
			$str .=$k."='".$v."',";
		}

		$str = rtrim($str,',');

		// var_dump($str);

		$sql ="update ".$this->table." set ".$str.$this->where;

		return $this->exec($sql);

	}
	/**
	 * [delete 删除功能]
	 * @Author   Xuebingsi
	 * @DateTime 2017-03-24T11:48:44+0800
	 * @param    [type]                   $id [description]
	 * @return   [type]                       [description]
	 */
	public function delete($id=null)
	{
		$key = $this->getPri();

		if($id){
			$sql  = "delete from ".$this->table." where ".$key."=".$id;
		}else{
			$sql  = "delete from ".$this->table.$this->where;
		}

		return $this->exec($sql);
	}

	/**
	 * [getPri 获取主键]
	 * @Author   Xuebingsi
	 * @DateTime 2017-03-24T11:48:56+0800
	 * @return   [type]                   [description]
	 */
	public function getPri()
	{
		$sql = "desc ".$this->table;

		$res = $this->query($sql);

		foreach ($res as $v) {
			if($v['Key']=='PRI'){
				return $v['Field'];
			}
		}
	}

	
}










 ?>